package cn.itlaobing.web.servlets;

import cn.itlaobing.dao.GoodsDao;
import cn.itlaobing.model.GoodsModel;
import cn.itlaobing.model.UserModel;
import cn.itlaobing.web.Constants;
import cn.itlaobing.web.annotation.RequestMapping;
import com.alibaba.druid.support.json.JSONUtils;
import org.apache.commons.collections.map.HashedMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * 订单信息展示页面
 * Created by xuaner on 2017/11/6.
 */
@WebServlet("/goods/*")
public class GoodsServlet extends BaseServlet {
    @RequestMapping("/show")
    public void show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        GoodsDao goodsDao=new GoodsDao();
        UserModel userModel=(UserModel) request.getSession().getAttribute(Constants.CURRENT_USER);
        try {
            List<GoodsModel> goodsList=goodsDao.queryAll(userModel.getUser_id());
            request.setAttribute("goodsList",goodsList);
            toView(request,response,"/goods/show");
            return;
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    /**
     * 支付状态的更改
     * */
    @RequestMapping("/payment")
    public void payment(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException{
        String goods_uid=request.getParameter("goods_id");
        GoodsDao goodsDao=new GoodsDao();
        UserModel um=(UserModel) request.getSession().getAttribute("Constants.CURRENT_USER");
        try {
            GoodsModel gs=goodsDao.payment(goods_uid);
            response.setContentType("application/json;charset=utf-8");
            if(gs==null){
                response.getWriter().write("");
                return;
            }
            Map<String,Object> result=new HashedMap();
            result.put("goods_id",gs.getGoods_id());
            result.put("goods_status",gs.getStatus().getText());
            response.getWriter().write(JSONUtils.toJSONString(result));
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
